<?php
class Astudio {
	public static function main($funcode) {
		switch ($funcode) {
			case 1:
				sview::verify();
				self::__doenterrooms();
				break;
			case 2:
				sview::verify();
				self::__doimconfig();
				break;
			case 3:
				sview::verify(true, true);
				self::__dousercharms();
				break;
			case 4:
				sview::verify();
				self::__dorecvstream();
				break;
			case 5:
				sview::verify();
				self::__dogoliveconfig();
				break;
			case 6:
				sview::verify(true, true);
				self::__dodefaultsetings();
				break;
			case 7:
				sview::verify(true, true);
				self::__dosubmitsetings();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//提交开播设置接口6-1-7
	private static function __dosubmitsetings() {
		$acl    = acl::getinst('vedio');
		$userid = $acl->user['id'];
		$url    = trim(sview::$ctrl['thumb']);
		if (!$url) {
			sview::$view->assign_error(80117, lang::text(80117));
		}
		$tagids  = trim(sview::$ctrl['tagids']);
		$airtime = (int)sview::$ctrl['airtime'];
		$title   = trim(sview::$ctrl['title']);
		$nlen    = mb_strlen($title, 'utf-8');
		if ($nlen > 50) {
			sview::$view->assign_error(80050, lang::text(80050));
		}
		if ($nlen > 0) {//验证屏蔽关键词
			$res = api_im::filter(2, $title);
			if ($res && $res['ResultContent'] == 1) {
				$lang = lang::load('filter.php');
				sview::$view->assign_error(80050, lang::replace(80088, $lang['title']));
			}
		}
		$isnewer = api_setting::submit(sview::$view, $tagids, $userid, $url, $title, $airtime);
		sview::$view->assign_by_ref('isnewer', $isnewer);
		sview::$view->display();
	}
	
	//用户开播设置获取基础信息6-1-6
	private static function __dodefaultsetings() {
		$acl    = acl::getinst('vedio');
		$userid = (int)$acl->user['id'];
		$where  = array('userid'=>$userid);
		$studio = model::load('live/studio')->get_item($where);
		if (!$studio || $studio['status'] != 1) {//判断直播间异常
			sview::$view->assign_error(80082, lang::text(80082));
		}
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if (!$user || !$user['emailverify']) {
			sview::$view->assign_error(80114, lang::text(80114));
		}
		$title  = empty($studio['title'])? '':stripslashes($studio['title']);
		$tags   = json_decode($studio['tags'], true);
		$link   = router::get_base_url().router::get_url('live', $studio['studiono']);
		$nick   = htmlspecialchars_decode($user['nickname']);
		//获取在线用户数量
		$data   = array('userid'=>$userid, 'studioid'=>$studio['id'], 'nickname'=>$nick,
			'avtar'=>$user['thumb'], 'thumb'=>$studio['thumb'], 'title'=>$title, 'link'=>$link,
			'airtime'=>$studio['airtime'], 'tags'=>is_array($tags)? $tags:array());
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//直播间信息6-1-1
	private static function __doenterrooms() {
		sview::$ctrl['studioid'] = intval(sview::$ctrl['studioid']);
		//判断直播间是否存在 不存在跳转
		$studio = model::load('live/studio')->get_one(sview::$ctrl['studioid']);
		if (!$studio || $studio['status'] != 1) {
			sview::$view->assign_error(80082, lang::text(80082));
		}
		$user   = model::load('live/userinfo')->set_devision($studio['userid'])->get_one($studio['userid']);
		$anchor = model::load('live/anchor')->get_one($studio['userid']);
		$title  = stripslashes($studio['title']);
		$acl    = acl::getinst('vedio');
		$isfans = 0;
		$userid = 0;
		$isfeed = 0;
		if ($acl->user) {
			$userid  = (int)$acl->user['id'];
			if ($userid != $studio['userid']) {
				$isfans  = api_stats::hattens($userid, $studio['userid']);
				if ($studio['airtime'] > config::$nowtime) {
					$feed    = api_studio::feed($userid, $studio['id']);
					$isfeed  = isset($feed[$studio['id']])? 1:0;
				}
				api_studio::visit($userid, $studio);
			}
		}
		//遍历获取拉流地址
		$evtid  = (int)api_studio::geteid($studio['id'], $studio['userid']);
		$starlv = api_studio::grade($anchor['starlv'], $studio['iscurated']);
		$link   = router::get_base_url().router::get_url('live', $studio['studiono']);
		$nick   = htmlspecialchars_decode($user['nickname']);
		$city   = htmlspecialchars_decode($user['city']);
		$birth  = empty($user['birth'])? strtotime('1991-01-01'):strtotime($user['birth']);
		$data   = array('userid'=>$studio['userid'], 'studioid'=>$studio['id'], 'nickname'=>$nick,
			'avtar'=>$user['thumb'], 'lv'=>$user['lv'], 'exp'=>$user['exp'], 'thumb'=>$studio['thumb'], 
			'factor'=>4, 'charms'=>$anchor['charms'], 'olstatus'=>$studio['olstatus'], 'link'=>$link,
			'airtime'=>$studio['airtime'], 'neartime'=>$studio['neartime'], 'eventid'=>$evtid,
			'title'=>$title, 'isfollow'=>$isfans, 'isfeed'=>$isfeed, 'iscurated'=>$starlv,'gender'=>$user['sex'],
			'birthday'=>$birth, 'location'=>$city, 'isbirth'=>$user['isbirth']
		);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//请求获取IM连接信息6-1-2
	private static function __doimconfig() {
		$acl    = acl::getinst('vedio');
		$userid = ($acl->user)? (int)$acl->user['id']:0;
		$config = config::load('im', false);
		$ims    = array();
		$nlen   = count($config['im']);
		$rlen   = ($nlen < 3)? $nlen:3;
		$idx    = config::$nowtime % $nlen;
		for ($i = 0; $i < $rlen; $i++) {
			list($ip, $port) = explode(':', $config['im'][$idx]);
			$ims[] = array($ip, intval($port));
			$idx   = ($idx + 1) % $nlen;
		}
		$im     = array('ims'=>$ims, 'iuin'=>0, 'nick'=>'Anonymous');
		if ($userid > 0) {
			$user = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
			$im['iuin']   = $userid;
			$im['nick']   = $user['nickname'];
			$im['token']  = md5('T15R2d'.$user['loginpw'].'3748sf');
			api_im::regtoken($im['iuin'], $im['iuin'], $im['token']);
		} else {
			$im['token']  = md5($_SERVER['HTTP_SIGNATURE']);
			$im['iuin']   = api_im::INTBASE + input::strhash($_SERVER['HTTP_SIGNATURE']);
			api_im::regtoken($im['iuin'], 0, $im['token']);
		}
		sview::$view->assign('datasets', $im);
		sview::$view->display();
	}
	
	//根获取当前用户的魅力信息 6-1-3
	private static function __dousercharms() {
		$acl    = acl::getinst('vedio');
		$userid = (int)$acl->user['id'];
		$data   = api_charms::get($userid);
		if ($data['dtoday']!= intval(config::$nowtime/24/3600)) {
			$data['dcharms']= 0;
		}
		$status     = 0;
		$lcharms    = api_charms::$lcharms;
		if ($data['charms'] >= $lcharms || intval($data['dcharms']) >= $lcharms) {
			$status = 1;
		}
		$balance    = 0;
		$finance	= model::load('live/finance')->set_devision($userid)->get_one($userid);
		$balance 	= $finance['lastcoin'];
		
		$data = array('charms'=>(int)$data['charms'], 'balance'=>$balance,
				'status'=>$status, 'timeout'=>(int)$data['airtime']);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//直播间历史回播6-1-4
	private static function __dorecvstream() {
		$sid    = intval(sview::$ctrl['studioid']);
		$studio = model::load('live/studio')->get_one($sid);
		if (!$studio) {
			sview::$view->assign_error(80082, lang::text(80082));
		}
		$rtmp   = array();
		$m3u8   = array();
		if ($studio['olstatus'] == 1) {
			$evtid= (int)api_studio::geteid($studio['id'], $studio['userid']);
			$rtmp = api_rtmp::recv($studio['id'], $evtid);
		} else {
			$m3u8 = api_video::vodlist($studio['userid'], 1, 0, 4);
		}
		
		$data   = array('airtime'=>$studio['airtime'], 'olstatus'=>$studio['olstatus'],
			'rtmp'=>empty($rtmp)? array():$rtmp, 'm3u8'=>empty($m3u8)? array():$m3u8);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//获取贴图的资源6-1-5
	private static function __dogoliveconfig() {
		$where  = array('status'=>1);
		$field  = 'name, ckey, icon';
		$filter = model::load('live/efilter')
			->get_only_list(0, -1, $where, $field, 'rank', orm::ASC);
		$chartlet = model::load('live/chartlet')
			->get_only_list(0, -1, $where, 'src,icon', 'rank', orm::ASC);
		$data = array('chartlet'=>$chartlet, 'filter'=>$filter);
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
}